package com.zs.grain.tool;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;

import com.zs.grain.R;
import com.xnkj1688.semplibrary.dialog.DialogResult;

public class XDialog extends Dialog implements View.OnClickListener{

    DialogResult dialogResult;
    Handler mHandler;

    private  Context context;
    private int height,width;
    private boolean cancelTouchout;
    private View view;

    public XDialog(Builder builder) {
        super(builder.context);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }
    public XDialog(Builder builder, int theme) {
        super(builder.context, theme);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }

    public XDialog(Context context, int theme) {
        super(context, theme);
    }
//    public XDialog(Context context, boolean cancelable
//            , OnCancelListener cancelListener) {
//        super(context, cancelable, cancelListener);
//    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_yes_no);//view

//        setCanceledOnTouchOutside(cancelTouchout);
//        Window win = getWindow();
//        WindowManager.LayoutParams lp = win.getAttributes();
//        lp.gravity = Gravity.CENTER;
//        lp.height = height;
//        lp.width = width;
//        win.setAttributes(lp);
//
//        view.findViewById(R.id.noButton).setOnClickListener(new Button.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                dismiss();
//            }
//        });

        //TODO:点击后无法消失 ->模拟点击屏幕某处使显示框隐藏
        //TODO:获取正在操作的反馈结果
        //TODO:toast
        //TODO:psw
        initArguments();
        initViews();
    }

    public static final class Builder {

        private Context context;
        private int height, width;
        private boolean cancelTouchout;
        private View view;
        private DialogInterface view2;
        private int resStyle = -1;


        public Builder(Context context) {
            this.context = context;
        }

        public Builder view(int resView) {
            view = LayoutInflater.from(context).inflate(resView, null);
            return this;
        }

        public Builder heightpx(int val) {
            height = val;
            return this;
        }

        public Builder widthpx(int val) {
            width = val;
            return this;
        }

        public Builder heightdp(int val) {
            height = (int)(val * context.getResources().getDisplayMetrics().density);//DensityUtil.dip2px(context, val);
            return this;
        }

        public Builder widthdp(int val) {

            width = (int)(val * context.getResources().getDisplayMetrics().density);//DensityUtil.dip2px(context, val);
            return this;
        }

        public Builder heightDimenRes(int dimenRes) {
            height = dimenRes;//context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder widthDimenRes(int dimenRes) {
            width = dimenRes;//context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder style(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        public Builder cancelTouchout(boolean val) {
            cancelTouchout = val;
            return this;
        }

        public Builder addViewOnclick(int viewRes, View.OnClickListener listener){
            view.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }
//        public Builder addViewOnclick(int viewRes,View.OnClickListener listener){
//            view.findViewById(viewRes).setOnClickListener(listener);
//            return this;
//        }

        public Builder dismissAfterClick(boolean isDismiss){
            //if(isDismiss) dismiss();//view.findViewById(viewRes).setOnClickListener(context);
            return this;
        }

        public XDialog build() {
            if (resStyle != -1) {
                return new XDialog(this, resStyle);
            } else {
                return new XDialog(this);
            }
        }
    }

    private void initArguments() {
    }
    private void initViews() {
        findViewById(R.id.yesButton).setOnClickListener(this);
        findViewById(R.id.noButton).setOnClickListener(this);
    }

    @Override
    protected void onStart() {

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.yesButton) {
            endDialog(DialogResult.OK);
        } else if (i == R.id.noButton) {
            endDialog(DialogResult.CANCEL);
        }
    }

    public void setDialogResult(DialogResult dialogResult) {
        this.dialogResult = dialogResult;
    }

    public synchronized void endDialog(DialogResult result) {
        if (mHandler != null) {
            setDialogResult(result);
            Message m = mHandler.obtainMessage();
            mHandler.sendMessage(m);
            mHandler = null;
        }
        dismiss();
    }

    public DialogResult showDialog() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message mesg) {
                throw new RuntimeException("endDialog");
            }
        };
        this.setCancelable(false);
        super.show();
        try {
            Looper.getMainLooper().loop();
        } catch (RuntimeException e) {
            mHandler = null;
//            if (!"endDialog".equals(e.getMessage()))
//                throw e;
        }
        return dialogResult;
    }
}
